var dateTimePicker = require('./dateTimePicker.js');

function timeOnLoad(type = 'default', date) {
  var startYear = 1980,
    endYear = 2050,
    obj,
    obj1;
  // 获取完整的年月日 时分秒，以及默认显示的数组
  if (type === 'max') {
    obj = dateTimePicker.dateTimePickerMax(startYear, endYear, date);
    obj1 = dateTimePicker.dateTimePickerMax(startYear, endYear, date);
  } else {
    obj = dateTimePicker.dateTimePicker(startYear, endYear, date);
    obj1 = dateTimePicker.dateTimePicker(startYear, endYear, date);
  }
  // 精确到分的处理，将数组的秒去掉
  var lastArray = obj1.dateTimeArray.pop();
  var lastTime = obj1.dateTime.pop();
  return {
    dateTimeSec: obj.dateTime,
    dateTimeArraySec: obj.dateTimeArray,
    dateTimeArrayMin: obj1.dateTimeArray,
    dateTimeUnitArrayMin: obj1.dateTimeUnitArray,
    dateTimeMin: obj1.dateTime
  }
}

function changeDateTimeSec(e) {
  if (e.detail.value) {
    return e.detail.value
  } else {
    return ''
  }
}

function changeDateTimeMin(e) {
  if (e.detail.value) {
    return e.detail.value
  } else {
    return ''
  }
}

function changeDateTimeColumn(e) {
  if (e.detail.value) {
    var arr = this.data.date.dateTimeSec,
      dateArr = this.data.date.dateTimeArraySec;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    return {
      dateTimeArraySec: dateArr,
      dateTimeSec: arr
    }
  } else {
    return {
      dateTimeArraySec: '',
      dateTimeSec: ''
    }
  }
}

function changeDateTimeColumn1(e) {
  if (e.detail.value) {
    var arr = this.data.date.dateTimeMin,
      dateArr = this.data.date.dateTimeArrayMin;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    return {
      dateTimeArrayMin: dateArr,
      dateTimeMin: arr
    }
  } else {
    return {
      dateTimeArrayMin: '',
      dateTimeMin: ''
    }
  }
}

module.exports = {
  timeOnLoad: timeOnLoad,
  changeDateTimeSec: changeDateTimeSec,
  changeDateTimeMin: changeDateTimeMin,
  changeDateTimeColumn: changeDateTimeColumn,
  changeDateTimeColumn1: changeDateTimeColumn1
}